using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hook : MonoBehaviour
{
    public static Hook Instance;
    public Transform startTrans;    //起始点
    LineRenderer lineRenderer;
    public bool isFire;
    public bool isBack;
    public Vector3 playStartPoint;
    public float moveSpeed;
    public float startSpeed;

    private void Awake()
    {
        Instance = this;
    }

    void Start()
    {
        isFire = false;
        isBack = false;
        lineRenderer = GetComponent<LineRenderer>();
        lineRenderer.startWidth = 0.1f;//修改线条宽度
        startSpeed = moveSpeed;
    }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
        {
            if (!isFire)
            {
                isFire = true;
                playStartPoint = transform.position;
            }
        }

        // 检测触摸输入
        if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0); // 获取第一个触摸点

            // 如果触摸开始
            if (touch.phase == TouchPhase.Began)
            {
                if (!isFire)
                {
                    isFire = true;
                    playStartPoint = transform.position;
                }
            }
        }


        if (isFire && !isBack)
        {
            PlayMoveForward();
        }
        else if (isFire && isBack)
        {
            PlayBackMove();
        }


        if (!isFire)
        {
            moveSpeed = startSpeed;
            PlayRotate();
        }
        UpdataLine();
    }
    public void UpdataLine()
    {
        lineRenderer.SetPosition(0, startTrans.position);
        lineRenderer.SetPosition(1, transform.position);//设置线条2点的位置c
    }

    //定义旋转方向枚举
    public enum RotaDir
    {
        left,
        right,
    }

    public RotaDir nowDir;    //玩家当前的旋转方向
    public float angleSpeed;  //旋转速度
    public void PlayRotate()
    {

        float rightAngle = Vector3.Angle(transform.up, Vector3.right);//计算玩家前进方向与Right的夹角


        if (nowDir == RotaDir.left)
        {
            if (rightAngle < 170)
            {   //在可旋转范围内按当前方向继续旋转
                transform.RotateAround(startTrans.position, Vector3.forward, angleSpeed * Time.deltaTime);
            }
            else
            {
                nowDir = RotaDir.right;//超出范围，改变方向进行旋转
            }

        }
        else
        {
            if (rightAngle > 10)
            {
                transform.RotateAround(startTrans.position, Vector3.forward, -angleSpeed * Time.deltaTime);
            }
            else
            {
                nowDir = RotaDir.left;
            }

        }
    }

    public GameObject item;
    public void PlayMoveForward()//前向移动
    {
        transform.position += transform.up * -1 * moveSpeed * Time.deltaTime;
    }
    public void PlayBackMove()//返回移动
    {
        transform.position += transform.up * moveSpeed * Time.deltaTime;
        if (transform.position.y >= playStartPoint.y)
        {
            transform.position = playStartPoint;
            isFire = false;
            isBack = false;
            if (item)
            {
                UIManager.Instance.SetScore(item.GetComponent<Item>().money);
                AudioManager.Instance.PlaySFX("加钱");
                Destroy(item);
                item = null;
            }
        }
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("Boundary"))
        {
            isBack = true;
        }

        if (other.CompareTag("Item") && item == null)
        {
            AudioManager.Instance.PlaySFX("拉绳");
            item = other.gameObject;
            isBack = true;
            item.transform.parent = transform;
            item.transform.position = transform.TransformPoint(Vector3.zero); // 将局部原点转换为世界坐标并设置位置
            item.transform.rotation = transform.rotation; // 将旋转角度设置为当前对象的旋转角度
            ComputeSpeed();
        }
    }

    public void ComputeSpeed()//计算新的速度
    {
        int scaleLevel = item.GetComponent<Item>().scaleLevel + 1;
        float deceleration = item.GetComponent<Item>().deceleration;
        moveSpeed = deceleration / scaleLevel;
    }

    //重置
    public void Reset()
    {
        transform.position = playStartPoint;
        isFire = false;
        isBack = false;
        if (item)
        {
            Destroy(item);
            item = null;
        }
    }
}
